<?php
namespace app\admin\model;

use think\Model;

class Variable extends Model
{
	protected $autoWriteTimestamp = true;
	protected $updateTime = false;

	protected $type = [
		//'title'	=>	'int',
	];


	public function addConfig()
	{
		//
	}

	public function addVariable(array $params)
	{

		$save = $this->save([
			'key'			=> $params['key'],
			'admin_title'	=> $params['admin_title'],
			'admin_desc'	=> $params['admin_desc'],
			'admin_type'	=> $params['admin_type'],
			'status'		=> $params['status'],
		]);

		if ($save === false) {
		return false;
		}

		return $params['key'];
	}

	public static function webConfig()
	{
		//返回标准的 k -> value 类型 status 必须为1 [可用]
		return self::field('key,value,content')->where('status',1);
	}

	public function updateList(array $params)
	{
		//以列表的形式保存更新(也就是没有底层权限更新)
		$lists = $this->reBuildUpList($params);

		return $this->saveAll($lists);

	}

	protected function reBuildUpList(array $params)
	{
		$new = [];
        foreach ($params as $key => $value) {
            $new[] = [
            'id' => (int) substr($key,2),
            'value' => $value,
            ];
        }
		//echo json_encode($new);
		return $new;
	}

	public function setValueAttr($value,$data)
	{
		if(is_array($value))
		{
			return json_encode(array_keys($value));
		}
		return $value;
	}
}